--
-- Author: shunguo.chen
-- Date: 2017-6-29 13:42:49
--
-- SettingIMPanel

local TabsBtn = require("app.widget.TabsBtn")
local LangMenuRender = import(".LangMenuRender")

local ClassRef = app.mvc.createPanelClass(...)



function ClassRef.create(node)
	local uiName = Res.SettingIMENPanel
	local publisher = app:getPublisher()
	if publisher == Enums.Publisher.tencent then --tencent
		uiName = Res.SettingIMPanel
	end
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(uiName))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init()
	return sprite
end


function ClassRef:_getCurLangConfig(lang)
	if not lang then
		lang = app:getCurrentLanguage()
	end
	for k , v in pairs(self._langConfig) do
		if v.key == lang then
			return v
		end
	end
end

function ClassRef:init(data)
	self.super.init(self, data)
	local setting = UD.setting
	 self:_initLangSetting()
	-- init btns
	for i=1,13 do
		local btn = self._ui["Panel/Panel_" .. i .. "/selected_btn"]
		btn:setTag(i)
		btn:addClickEventListener(handler(self, self.onBtnClick))
		if i == 1 then
			btn:setBright(not setting:isAutoPlayAudioWorld())
		elseif i == 2 then
			btn:setBright(not setting:isAutoPlayAudioTeam())
		elseif i == 3 then
			btn:setBright(not setting:isAutoPlayAudioGuild())
		elseif i == 4 then
			btn:setBright(not setting:isShowWorldBubble())
		elseif i == 5 then
			btn:setBright(not setting:isShowTeamBubble())
		elseif i == 6 then
			btn:setBright(not setting:isShowGuildBubble())
		elseif i == 7 then
			btn:setBright(not setting:isShowTeamreqBubble())
		elseif i <= 13 then
			local index = i - 7 --聊天综合频道设置从1开始
			btn:setBright(not setting:getImChannelInfoSet(index))
		end
	end
end

-- 	1	世界-自动播放
-- 	2	队伍-自动播放
-- 	3	工会-自动播放
-- 	4	世界-头顶气泡
-- 	5	队伍-头顶气泡
-- 	6	工会-头顶气泡
-- 	7	招募-头顶气泡
--  8-13聊天综合频道设置
function ClassRef:onBtnClick(sender)
	local tag = sender:getTag()
	local isBright = sender:isBright()
	sender:setBright(not isBright)
	local setting = UD.setting
	if tag == 1 then
		setting:setAutoPlayAudioWorld(isBright)
	elseif tag == 2 then
		setting:setAutoPlayAudioTeam(isBright)
	elseif tag == 3 then
		setting:setAutoPlayAudioGuild(isBright)
	elseif tag == 4 then
		setting:setShowWorldBubble(isBright)
	elseif tag == 5 then
		setting:setShowTeamBubble(isBright)
	elseif tag == 6 then
		setting:setShowGuildBubble(isBright)
	elseif tag == 7 then
		setting:setShowTeamreqBubble(isBright)
	elseif tag <= 13 then
		local index = tag - 7
		setting:setImChannelInfoSet(index, isBright)
		EM:notify("onSettingImChannelInfo", index, isBright)
	end
	UD:updateSetting(setting)
end

function ClassRef:_createSettingLangRender()
	self._langSettingRender = LangMenuRender.create()
	self._langSettingRender:setData(self._langConfig)
	self._langSettingRender:setVisible(false)
	self._langSettingRender:setAnchorPoint(0, 1)
	display.nodeRemoveForNotHit(self._langSettingRender, function()
		if self._langSettingRender and self._langSettingRender:isVisible() then
			self._langSettingRender:setVisible(false)
			self._ui["Panel/Panel_top/btn_more"]:setFlippedY(false)
		end
	end)
	self._ui["Panel/Panel_top/bg"]:addChild(self._langSettingRender)
	self._langSettingRender:setCallback(handler(self, self._onSelectLang))
end

function ClassRef:_onSelectLang(langConfig)
	if self._curLangConfig.key ~= langConfig.key then
		self._ui["Panel/Panel_top/language"]:setString(langConfig.name)
		local confirmDialog = display.newConfirmDialog(string.format(L("ChangeDefaultLang"), langConfig.name), function()
			app:changeDefaultLanguage(langConfig.key)
		end, function()
			self._ui["Panel/Panel_top/language"]:setString(self._curLangConfig.name)
		end)
		confirmDialog:setConfirmText(L("CommonBtnConfirm"))
		confirmDialog:setCancelText(L("CommonBtnCancel"))
		cc.Director:getInstance():getRunningScene():addChild(confirmDialog, 1000)
	end
end




function ClassRef:_initLangSetting()

	local LangsString = {
		CHS = L("ChineseSimplifiedLang"),
		EN = L("EnglishLang")
	}
	local langArr = app:getMultiLanguageArray()
	local curLang = app:getCurrentLanguage()

	self._langConfig = {}

	local isContainCurLang = false;
	for k ,v in pairs(langArr) do
		if not isContainCurLang and v == curLang then
			isContainCurLang = true
		end
		table.insert(self._langConfig, {name = LangsString[v] or "", key = v});
	end

	if not isContainCurLang then
		table.insert(self._langConfig, {name = LangsString[curLang] or "", key = curLang});
	end

	self._curLangConfig = self:_getCurLangConfig()
	if not self._curLangConfig then
		self._curLangConfig = self:_getCurLangConfig("CHS")
	end

	if #self._langConfig <= 1 then
		self._ui["Panel/Panel_top/btn_more"]:setVisible(false)
	else
		self._ui["Panel/Panel_top/btn_more"]:setVisible(true)
	end
	self._ui["Panel/Panel_top/language"]:setString(self._curLangConfig.name)

	self._ui["Panel/Panel_top/btn_more"]:addClickEventListener(function()
		if not self._langSettingRender then
			self:_createSettingLangRender()
		end
		if self._langSettingRender:isVisible() then
			self._langSettingRender:setVisible(false)
			self._ui["Panel/Panel_top/language"]:setVisible(true)
			self._ui["Panel/Panel_top/btn_more"]:setFlippedY(false)
		else
			self._langSettingRender:setVisible(true)
			self._ui["Panel/Panel_top/language"]:setVisible(false)
			self._ui["Panel/Panel_top/btn_more"]:setFlippedY(true)
		end
	end)

end

return ClassRef